using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UFrame.Audio;
using System;

public class TableSoundCtrl : AudioPlayCtrl, UFrame.IRegistAble
{
    public void OnRegist()
    {

    }

    public void OnUnRegist()
    {
        if (m_audioLoader != null)
            m_audioLoader.ClearAllLoaded();
    }

    public AudioContext PlayAudioById(int audioId)
    {
        var audioCfg = TableAgent.Instance.GetSoundCfgById(audioId);
        if (audioCfg != null)
        {
            if (audioCfg.is_bgm)
            {
                return Audio2D.PlayMusic(audioCfg.path, 0, true);
            }
            else
            {
                return Audio2D.PlaySFX(audioCfg.path);
            }
        }
        return null;
    }

    public AudioContext PlayAudioById(int audioId, Vector3 pos, float pitch = 1)
    {
        var audioCfg = TableAgent.Instance.GetSoundCfgById(audioId);
        if (audioCfg != null)
        {
            if (audioCfg.is_bgm)
            {
                return Audio2D.PlayMusic(audioCfg.path, 0, true);
            }
            else
            {
                return Audio3D.PlaySFX(audioCfg.path, pos, pitch: pitch);
            }
        }
        return null;
    }

    public AudioContext PlayAudioById(int audioId, int chanel, float volume, float pitch)
    {
        var audioCfg = TableAgent.Instance.GetSoundCfgById(audioId);
        if (audioCfg != null)
        {
            return Audio2D.PlayMusic(audioCfg.path, chanel, true, volume);
        }
        return null;
    }


    public void ClearCache()
    {
        m_audioLoader.ClearAllLoaded();
        Resources.UnloadUnusedAssets();
    }
}